#include "mainwindow.h"
#include<QCompleter>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    keywordLabel = new QLabel("Keyword", this);
    keywordLabel->setGeometry(10, 10, 80, 30);

    keywordLineEdit = new QLineEdit(this);
    keywordLineEdit->setGeometry(100, 10, 200, 30);
    QStringList keywordList;
    keywordList << "apple" << "banana" << "cat" << "dog"; // 设置输入提示的关键词列表
    QCompleter *completer = new QCompleter(keywordList, this);
    completer->setCaseSensitivity(Qt::CaseInsensitive);
    keywordLineEdit->setCompleter(completer);

    searchButton = new QPushButton("Search", this);
    searchButton->setGeometry(310, 10, 80, 30);
    connect(searchButton, &QPushButton::clicked, this, &MainWindow::searchButtonClicked);

    resultTextEdit = new QTextEdit(this);
    resultTextEdit->setGeometry(10, 50, 380, 240);
}

MainWindow::~MainWindow()
{
    delete keywordLabel;
    delete keywordLineEdit;
    delete searchButton;
    delete resultTextEdit;
}

void MainWindow::searchButtonClicked()
{
    QString keyword = keywordLineEdit->text();
    QString textToSearch = "This is an example text. It contains the keyword apple.";

    // 查找关键词
    int index = textToSearch.indexOf(keyword, 0, Qt::CaseInsensitive);
    if (index != -1) {
        resultTextEdit->setText("Keyword found at position: " + QString::number(index));
    } else {
        resultTextEdit->setText("Keyword not found.");
    }
}

